package com.learn.shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.shop.constant.DictEnum;
import com.learn.shop.pojo.entriy.SDictData;
import com.learn.shop.mapper.SDictDataMapper;
import com.learn.shop.pojo.vo.SDictDataVo;
import com.learn.shop.service.SDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 字典(SDictData)表服务实现类
 *
 * @author zhengdechang
 * @since 2021-12-20 16:31:02
 */
@Service
@Slf4j
public class SDictDataServiceImpl implements SDictDataService {

    private static final Logger logger = LoggerFactory.getLogger(SDictDataServiceImpl.class);

    @SuppressWarnings("all")
    @Autowired
    private SDictDataMapper sDictDataMapper;

    @Override
    public PageInfo<SDictData> list(SDictDataVo vo) {

        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<SDictData> list = sDictDataMapper.list(vo.getQuery());
        list.forEach(dto -> {
            dto.setDictType(DictEnum.getDictTypeDesc(dto.getDictType()));
        });
        return new PageInfo<>(list);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SDictData queryById(Integer id) {
        SDictData sDictData = this.sDictDataMapper.queryById(id);
        sDictData.setDictType(DictEnum.getDictTypeDesc(sDictData.getDictType()));
        return sDictData;
    }

    /**
     * 分页查询
     *
     * @param sDictData   筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<SDictData> queryByPage(SDictData sDictData, PageRequest pageRequest) {
        long total = this.sDictDataMapper.count(sDictData);
        return new PageImpl<>(this.sDictDataMapper.queryAllByLimit(sDictData, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param sDictData 实例对象
     * @return 实例对象
     */
    @Override
    public SDictData insert(SDictData sDictData) {
        this.sDictDataMapper.insert(sDictData);
        return sDictData;
    }

    /**
     * 修改数据
     *
     * @param sDictData 实例对象
     * @return 实例对象
     */
    @Override
    public SDictData update(SDictData sDictData) {
        this.sDictDataMapper.update(sDictData);
        return this.queryById(sDictData.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.sDictDataMapper.deleteById(id) > 0;
    }
}
